import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class Record {
    static Map<String, Integer> monthlyIncome = new HashMap<>();
    static Map<String, Integer> monthlyExpense = new HashMap<>();
    static int budget = 0; // 总预算

    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        String data = "";
        int money = 0;
        String type = "";
        String tip = "";
        String month = "";

        System.out.println("请输入设置的月度预算：");
        budget = sca.nextInt();

        while (true) {
            System.out.println("\n请选择记录收入或者支出：");
            System.out.println("收入（1）；");
            System.out.println("支出（2）；");
            System.out.println("查看月度统计（3）；");
            System.out.println("退出（4）；");
            int choice = sca.nextInt();

            switch (choice) {
                case 1: {
                    System.out.println("请录入收入的日期（格式：yyyy-MM-dd）：");
                    data = sca.next();
                    month = data.substring(0, 7); // 获取年月
                    System.out.println("请录入收入的金额：");
                    money = sca.nextInt();
                    System.out.println("请录入收入的类别：");
                    type = sca.next();
                    System.out.println("请录入收入的备注：");
                    tip = sca.next();
                    monthlyIncome.put(month, monthlyIncome.getOrDefault(month, 0) + money);
                    System.out.println("收入已录入：日期:" + data + "；金额：" + money + "；类别：" + type + "；备注：" + tip);
                    break;
                }
                case 2: {
                    System.out.println("请录入支出的日期（格式：yyyy-MM-dd）：");
                    data = sca.next();
                    month = data.substring(0, 7); // 获取年月
                    System.out.println("请录入支出的金额：");
                    money = sca.nextInt();
                    if (money > budget - getTotalExpense()) {
                        System.out.println("警告！支出超出了预算！");
                    } else {
                        System.out.println("请录入支出的类别：");
                        type = sca.next();
                        System.out.println("请录入支出的备注：");
                        tip = sca.next();
                        monthlyExpense.put(month, monthlyExpense.getOrDefault(month, 0) + money);
                        System.out.println("支出已录入：日期:" + data + "；金额：" + money + "；类别：" + type + "；备注：" + tip);
                    }
                    break;
                }
                case 3: {
                    printMonthlyReport();
                    break;
                }
                case 4:
                    System.out.println("退出系统。");
                    return;
                default:
                    System.out.println("无效输入，请重新选择。");
            }
        }
    }

    public static void printMonthlyReport() {
        System.out.println("\n月度统计：");
        for (String month : monthlyIncome.keySet()) {
            int income = monthlyIncome.get(month);
            int expense = monthlyExpense.getOrDefault(month, 0);
            int balance = income - expense;
            System.out.println("月份：" + month);
            System.out.println("收入：" + income);
            System.out.println("支出：" + expense);
            System.out.println("余额：" + balance);
            System.out.println("预算：" + budget);
            System.out.println("剩余预算：" + (budget - getTotalExpense()));
            System.out.println();
        }
    }

    public static int getTotalExpense() {
        int totalExpense = 0;
        for (int expense : monthlyExpense.values()) {
            totalExpense += expense;
        }
        return totalExpense;
    }
}